-
Notifications
You must be signed in to change notification settings - Fork 97
Adds runtime warning on divide-by-zero, fixes use of "out" and "where" in arctan2 #5204
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
ecb5af1 to
12f6386
Compare
ajpotts
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work!
adc19fa to
0e2e04e
Compare
tests/numpy/datetime_test.py
Outdated
| # metrics["ak_supported"] += 1 | ||
| # except RuntimeWarning: | ||
| # continue # this test can cause divide-by-zero, which would | ||
| # # also cause a following test to fail, so skip it |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you remove the commented out code please?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ooops. Done.
arkouda/numpy/numeric.py
Outdated
| x1 = int(x1) | ||
| if np.isscalar(x2): | ||
| if isinstance(x2, (bool, np.bool_)): | ||
| x1 = int(x2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here you have x1 = int(x2) but it should be x2 = int(x2).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep. Good catch.
| if np.isscalar(x1) and np.isscalar(x2): | ||
| if out is None: | ||
| return nparctan2(x1, x2) if where is None or where is True else np.float64(1.0) | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why does this return np.float64(1.0)? If it isn't possible to compute a sensible value I think we should just throw an error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I picked 1.0 arbitrarily, because numpy allows this, but returns a random value. Here's an example:
>>> import numpy as np
>>> x1 = 5.0 ; x2 = 7.0
>>> np.arctan2(x1,x2,where=False)
np.float64(0.0)
>>> np.arctan2(x1,x2,where=False)
np.float64(5.0)
>>> np.arctan2(x1,x2,where=False)
np.float64(5.0)
I can change this to throw an error, certainly. I didn't do that at first, since numpy doesn't.
Closes #5132. Closes #5149.
The binops in pdarrayclass.py now give a RuntimeWarning or a Floating Point Error (or nothing) if / or // cause a divide-by-zero, depending on the "divide" setting of ak.errstate.
Because the RuntimeWarning surfaced an issue in
arctan2, I've rewritten that function somewhat. It now has an "out" parameter matching numpy, and uses the "where" parameter correctly.Most of the original arctan2 function remains as
_arctan2_, because (subjective opinion here) I think that calling it as a separate function improves the readability of the code.In doing this rewriting, I removed some mypy ignores that are no longer needed.
I rewrote the unit tests for
arctan2to use our newer assert functions. I think this cleans up that code significantly. In the cases that use "where," I check both the returned result, and the returned "out" pdarray, since they should match.Update:
Since other tests were also causing divide-by-zero errors, I extended the divide-by-zero check to use
ak.errstate. I also bypassed additional tests in datetime once a divide-by-zero occurred, because they would also cause errors.